package com.ndpmedia.fb.pmd.configration.web.resource;

import com.alibaba.fastjson.JSON;
import com.ndpmedia.fb.pmd.configration.common.Page;
import com.ndpmedia.fb.pmd.configration.common.Result;
import com.ndpmedia.fb.pmd.configration.service.AppService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.*;

/**
 *
 */
@Path("app")
@Component
public class AppResource {

    /**
     * 日志对象
     */
    private static final Logger LOG = LoggerFactory.getLogger(AppResource.class);

    @Autowired
    AppService appServices;

    @GET
    @Path("list")
    @Produces(MediaType.APPLICATION_JSON)
    public String getList() {
        return list("");
    }


    @POST
    @Path("list")
    @Produces(MediaType.APPLICATION_JSON)
    public String postList(@FormParam("devname") String devname) {
        return list(devname);
    }

    private String list(String key) {
        Result result = null;
        try {
            List list = appServices.list(key);
            Page page = new Page(1, 1, list == null ? 0 : list.size(), 1);
            result = new Result(1, list, page, "");
        } catch (Exception e) {
            result = new Result(0, null, e.getMessage());
        }
        return JSON.toJSONString(result);
    }

    @GET
    @Path("add")
    @Produces(MediaType.APPLICATION_JSON)
    public String getAdd(@QueryParam("name") String name) {
        return add(name);
    }


    @POST
    @Path("add")
    @Produces(MediaType.APPLICATION_JSON)
    public String postAdd(@FormParam("name") String name) {
        return add(name);
    }

    private String add(String name) {
        Result result = null;
        try {
            result = new Result(1, appServices.add(name), "");
        } catch (Exception e) {
            result = new Result(0, null, e.getMessage());
        }
        return JSON.toJSONString(result);
    }

    @GET
    @Path("delete")
    @Produces(MediaType.APPLICATION_JSON)
    public String getDelete(@QueryParam("appid") String appid) {
        return delete(appid);
    }


    @POST
    @Path("delete")
    @Produces(MediaType.APPLICATION_JSON)
    public String postDelete(@FormParam("appid") String appid) {
        return delete(appid);
    }

    private String delete(String appid) {
        Result result = null;
        try {
            appServices.delete(appid);
            result = new Result(1, new Date().getTime() + "", "");
        } catch (Exception e) {
            result = new Result(0, null, e.getMessage());
        }
        return JSON.toJSONString(result);
    }

    @GET
    @Path("update")
    @Produces(MediaType.APPLICATION_JSON)
    public String getUpdate(@QueryParam("appid") String appid, @QueryParam("name") String name) {
        return update(appid, name);
    }


    @POST
    @Path("update")
    @Produces(MediaType.APPLICATION_JSON)
    public String postUpdate(@FormParam("appid") String appid, @FormParam("name") String name) {
        return update(appid, name);
    }

    private String update(String appid, String name) {
        Result result = null;
        try {
            appServices.update(appid, name);
            result = new Result(1, new Date().getTime() + "", "");
        } catch (Exception e) {
            result = new Result(0, null, e.getMessage());
        }
        return JSON.toJSONString(result);
    }

    @GET
    @Path("push")
    @Produces(MediaType.APPLICATION_JSON)
    public String getPush(@QueryParam("appid") String appid) {
        return push(appid);
    }


    @POST
    @Path("push")
    @Produces(MediaType.APPLICATION_JSON)
    public String postPush(@FormParam("appid") String appid) {
        return push(appid);
    }

    private String push(String appid) {
        Result result = null;
        try {
            appServices.push(appid);
            result = new Result(1, new Date().getTime() + "", "");
        } catch (Exception e) {
            result = new Result(0, null, e.getMessage());
        }
        return JSON.toJSONString(result);
    }

    @GET
    @Path("status")
    @Produces(MediaType.APPLICATION_JSON)
    public String getStatus(@QueryParam("appid") String appid, @QueryParam("status") String status) {
        return status(appid, status);
    }


    @POST
    @Path("status")
    @Produces(MediaType.APPLICATION_JSON)
    public String postStatus(@FormParam("appid") String appid, @FormParam("status") String status) {
        return status(appid, status);
    }

    @POST
    @Path("copy")
    @Produces(MediaType.APPLICATION_JSON)
    public String postCopyApp(@FormParam("sourceAppId") String sourceAppId, @FormParam("destAppId") String destAppId) {
        return copyApp(sourceAppId, destAppId);
    }

    private String copyApp(String sourceAppId,String destAppId){
        Result result = null;
        try{
            if(appServices.copy(sourceAppId,destAppId)){
                result = new Result(1, new Date().getTime() + "", "");
            }else{
                result = new Result(0, null,"应用名称重复");
            }
        }catch(Exception e) {
            result = new Result(0, null, e.getMessage());
        }
        return JSON.toJSONString(result);
    }

    private String status(String appid, String status) {
        Result result = null;
        try {
            appServices.status(appid, status);
            result = new Result(1, new Date().getTime() + "", "");
        } catch (Exception e) {
            result = new Result(0, null, e.getMessage());
        }
        return JSON.toJSONString(result);
    }
}
